﻿#include "confsettings.h"
#include <QApplication>
#include <QDir>
#include <QDomDocument>
#include <QFileInfo>
#include <QResource>
#include <QSettings>
#include <QTranslator>
#include <QUrl>
#include "usersettings.h"

QString ConfSettings::getTitleLabel()
{
    return QCoreApplication::translate("ConfSettings", titleLabel.toUtf8());
}

QString ConfSettings::getDisplayName()
{
    return QCoreApplication::translate("ConfSettings", displayName.toUtf8());
}

void ConfSettings::updateDomain(const QString &domain)
{
    if(domain.isEmpty())
        validDomain = appDomain;
    else
        validDomain = domain;
    if(validDomain.isEmpty())
        httpServer.isEmpty();
    else
        httpServer = validDomain + QLatin1String("/res/tempFileUpload");
}

void ConfSettings::updateProxy(const QString &domain, const QString &proxy)
{
    if(proxy.isEmpty())
    {
        if(domain.isEmpty() || domain == appDomain)
            validProxy = appProxy;
        else
            validProxy.clear();
    }
    else if(domain == validDomain || (domain.isEmpty() && validDomain == appDomain))
        validProxy = proxy;
    else
        validProxy.clear();
}

ConfSettings::ConfSettings() : defaultPhone(false) {
    if(QResource::registerResource(QApplication::applicationDirPath()+"/ui.rcc"))
    {
        QFile f(":/gui/ui.xml");
        if(f.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            QDomDocument doc;
            QString errorMsg;
            int errorLine = 0, errorColumn = 0;
            if(doc.setContent(&f, &errorMsg, &errorLine, &errorColumn))
            {
                auto root = doc.documentElement();
                titleLabel = root.firstChildElement("titleLabel").text();
                displayName = root.firstChildElement("displayName").text();
                appName = root.firstChildElement("appName").text();
                iconName = root.firstChildElement("iconName").text();
                updaterName = root.firstChildElement("updaterName").text();
                titleStyle = root.firstChildElement("titleStyle").text();
                auto text = root.firstChildElement("defaultPhone").text();
                if(!text.isEmpty())
                    defaultPhone = QVariant(text).toBool();
                //TODO:安装translation
            }
            f.close();
        }
    }
    if(QResource::registerResource(QApplication::applicationDirPath()+"/url.rcc"))
    {
        QFile f(":/url.xml");
        if(f.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            QDomDocument doc;
            QString errorMsg;
            int errorLine = 0, errorColumn = 0;
            if(doc.setContent(&f, &errorMsg, &errorLine, &errorColumn))
            {
                auto root = doc.documentElement();
                appDomain = root.firstChildElement("appDomain").text();
                appProxy = root.firstChildElement("appProxy").text();
                protocolVer = root.firstChildElement("protocolVer").text();
                mdsList = root.firstChildElement("mdsList").text();
                //top buttons
                webButtons.clear();
                auto child = root.firstChildElement("webButtons");
                if(!child.isNull())
                {
                    auto button = child.firstChildElement("button");
                    while (!button.isNull()) {
                        auto element = button.firstChildElement();
                        if(!element.isNull())
                        {
                            webButtons.append(QHash<QString, QString>());
                            auto &hash = webButtons.last();
                            while (!element.isNull()) {
                                hash[element.tagName()] = element.text();
                                element = element.nextSiblingElement();
                            }
                        }
                        button = button.nextSiblingElement("button");
                    }
                }
                //TODO:安装translation
            }
            f.close();
        }
    }
    QFileInfo fi(QApplication::applicationFilePath());
    fi.setFile(fi.dir(), fi.baseName()+QLatin1String(".ini"));
    if(fi.exists() && fi.isReadable())
    {
        QSettings app(fi.filePath(), QSettings::IniFormat);
        app.setIniCodec("utf-8");
        const QString group = QLatin1String("OverwriteConfigure");
        if(app.childGroups().contains(group))
        {
            app.beginGroup(group);
            const QStringList keys = app.childKeys();
            if(keys.contains("titleLabel"))
                titleLabel = app.value("titleLabel").toString();
            if(keys.contains("displayName"))
                displayName = app.value("displayName").toString();
            if(keys.contains("appName"))
                appName = app.value("appName").toString();
            if(keys.contains("iconName"))
                iconName = app.value("iconName").toString();
            if(keys.contains("updaterName"))
                updaterName = app.value("updaterName").toString();
            if(keys.contains("appDomain"))
                appDomain = app.value("appDomain").toString();
            if(keys.contains("appProxy"))
                appProxy = app.value("appProxy").toString();
            if(keys.contains("titleStyle"))
                titleStyle = app.value("titleStyle").toString();
            if(keys.contains("defaultPhone"))
                defaultPhone = app.value("defaultPhone").toBool();
            if(keys.contains("mdsList"))
                mdsList = app.value("mdsList").toString();
            if(keys.contains("protocolVer"))
                protocolVer = app.value("protocolVer").toString();
            app.endGroup();
        }
    }
}
